/*
  组件属性全局配置文件。优先级：全局配置文件props < 直接设置组件props
  目前支持配置的组件：fui-button、fui-icon、fui-text、fui-input、fui-form-item、fui-list-cell
*/

// 主色（V1.9.8+），仅Nvue端以及无法使用css变量控制颜色的组件使用【保持与fui-theme中一致】
function objectToUri(params) {
	let temp = ""
	Object.keys(params).forEach(key => temp += `&${key}=${params[key]}`)
	return temp
}

const color = {
	primary: '#0B9AFF',
	success: '#09BE4F',
	warning: '#FFB703',
	danger: '#FF2B2B',
	purple: '#6831FF',
	link: '#0B9AFF'
}

//全局方法（V1.9.8+）
const app = {
	//text：提示内容，icon：参考uni.showToast 中icon
	toast: function(text, icon = 'none') {
		text && uni.showToast({
			title: text,
			icon: icon,
			duration: 2000
		})
	},
	
	modal: function({ title = '', content, success, fail, showCancel = true, confirmColor, confirmText = '确定' }) {
		uni.showModal({
			title, content, showCancel, confirmText,
			// #ifndef MP-TOUTIAO
			cancelColor: "#7F7F7F",
			confirmColor: confirmColor || color.primary,
			// #endif
			success(res) {
				if (res.confirm) success && success(res)
				else fail && fail(res)
			},
			fail: error => fail && fail(error)
		})
	},
	
	// url:跳转页面路径，isMain：是否为tabbar内主页面
	href(url, params = null, isMain = false, islogin = true) {
		let { user } = uni.$store()
		if (params === 'login' || islogin && !user.islogin) {
			uni.navigateTo({ url: '/pages/account/login' })
			params !== 'login' && uni.showToast({title: '请先登录~', icon: 'none', duration: 2000 })
			return
		}
		
		if (isMain) uni.switchTab({ url })
		else {
			if (!params) uni.navigateTo({ url })
			else uni.navigateTo({ url: `${url}?${objectToUri(params)}` })
		}
	},
	
	/**
	 * @desc 等待多少毫秒再执行 ，同步阻塞
	 * @param {String} millisecond 毫秒
	 **/
	sleep(fun, millisecond = 300) {
		let now = new Date();
		let exitTime = now.getTime() + millisecond;
		while (true) {
			now = new Date();
			if (now.getTime() > exitTime) return fun && fun()
		}
	},
}

const fuiConfig = {
	//组件名称，小驼峰命名
	//如fui-button写成fuiButton
	fuiButton: {
		//组件属性值
		height: '85rpx',
		size: 29,
		radius: '16rpx'
	},
	fuiIcon: {
		size: 64,
		unit: 'rpx',
		//V1.9.8+
		color: ''
	},
	fuiText: {
		size: 32,
		unit: 'rpx',
		//仅Nvue有效，black 默认颜色，V1.9.8+
		color: ''
	},
	fuiInput: {
		labelSize: 32,
		size: 32
	},
	fuiFormItem: {
		labelSize: 32,
		labelWidth: 160,
		labelRight: 30,
		labelWeight: 400,
		labelAlign: 'left',
		asteriskPosition: 'left'
	},
	// V1.9.8+
	fuiListCell: {
		padding: ['32rpx', '32rpx'],
		arrowColor: '',
		//仅Nvue有效
		borderColor: '',
		bottomLeft: 32
	},
	// V1.9.9+
	fuiSection: {
		size: 32,
		color: '#181818',
		fontWeight: 600,
		descrSize: 28,
		descrColor: '#B2B2B2',
		descrTop: 12
	},
	color,
	...app
}

export default fuiConfig
